package com.xiaoyung.easyisapisdk.feign.api.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xiaoyung.easyisapisdk.feign.api.form.DataServiceApi;

public class EasyisOpenApi {
	private static Logger logger = LoggerFactory.getLogger(EasyisOpenApi.class);

	/*public static String EASYIS_OPENAPI_URL = "easyis.openapi.url";
	public static String EASYIS_OPENAPI_ACCESS_KEY = "easyis.openapi.accessKey";
	public static String EASYIS_OPENAPI_ACCESS_SECRET = "easyis.openapi.accessSecret";
	
	public static String EASYIS_OPENAPI_ACCESS_TOKEN = "easyis.openapi.accessToken";
	public static String EASYIS_OPENAPI_CAMLE_NAME_MODEL = "easyis.openapi.camleNameModel";*/
	
	private EasyisOpenApiClient client = null;//url,
	
	public EasyisOpenApi(JSONObject conf) {// String url,String accessKey ,String accessSecret,String accessToken,Boolean camleNameModel 
		/*String accessKey = System.getProperty(EASYIS_OPENAPI_ACCESS_KEY);
		String accessSecret = System.getProperty(EASYIS_OPENAPI_ACCESS_SECRET);
		System.out.println("static 初始化"+accessKey+" "+accessSecret);*/
		client = new EasyisOpenApiClient(conf);//url,
			
		this.dataService =  client.getApi(DataServiceApi.class);
	}
	
	public DataServiceApi dataService = null;
	
	//JSONObject conf = null;
	static public EasyisOpenApi getInstance() {//不要缓存,会造成第二次请求的tokne仍然用的是第一次的
		JSONObject conf = loadConfig();
		logger.debug("加载配置:{}",conf);
		EasyisOpenApi api = new EasyisOpenApi(conf);
		return api;
	}
	
	public static JSONObject loadConfig() {
		JSONObject confJson = new JSONObject();
		InputStream inputStream = EasyisOpenApi.class.getClassLoader().getResourceAsStream("easyisapi.conf");
		if(inputStream!=null) {
			try {
				String confStr = IOUtils.toString(inputStream,"UTF-8");
				confJson.putAll(JSON.parseObject(confStr));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		
		String easyisApiConf = System.getenv("EASYISAPI_CONF");
		if(!StringUtils.isEmpty(easyisApiConf)) {
			try {
				String confStr = FileUtils.readFileToString(new File(easyisApiConf), "UTF-8");
				confJson.putAll(JSON.parseObject(confStr));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		// camleNameModel
		
		String __openapi_url = System.getProperty("__openapi_url");
		if (!StringUtils.isEmpty(__openapi_url)) {
			String url = __openapi_url;// 内网地址优先调用
			confJson.put("url", url);
		}
		//从System.property中获取
		String __openapi_token = System.getProperty("__openapi_token");
		if (__openapi_token != null) {
			String accessToken = __openapi_token;
			confJson.put("accessToken", accessToken);
		}
		return confJson;
	}
	
	public static void main(String[] args)  {
		//JSONObject config = loadConfig();
		//InputStream inputStream = EasyisOpenApi.class.getClassLoader().getResourceAsStream("easyisapi.properties");
		//System.out.println(config);
	}
}
